package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicFinancialAgreement;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicRepaymentPlan;
import com.ruicar.afs.cloud.basic.common.service.BasicFinancialAgreementService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicRepaymentPlanService;
import com.ruicar.afs.cloud.basic.common.vo.FinanceProductInfoVO;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentPool;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentPoolService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.AssetTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.RepaymentStatusEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 金融产品基本信息
 */
@Slf4j
@RestController
@RequestMapping("/financeInfo")
@AllArgsConstructor
public class FinanceProductInfoController {
    private BasicMainInfoService basicMainInfoService;
    private BasicRepaymentPlanService basicRepaymentPlanService;
    private final BasicPaymentPoolService basicPaymentPoolService;
    private BasicFinancialAgreementService basicFinancialAgreementService;

    @ApiOperation(value = "根据合同号码查询金融产品信息")
    @PostMapping("/findByContractNo")
    public IResponse findByContractNo(@RequestParam("contractNo") String contractNo) {
        BigDecimal b = BigDecimal.valueOf(0);
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery().eq(BasicMainInfo::getContractNo, contractNo));
        List<BasicFinancialAgreement> basicFinancialAgreement = basicFinancialAgreementService.list(Wrappers.<BasicFinancialAgreement>lambdaQuery().eq(BasicFinancialAgreement::getContractNo, contractNo));
        List<BasicRepaymentPlan> repaidTermNo = basicRepaymentPlanService.list(Wrappers.<BasicRepaymentPlan>lambdaQuery()
                .eq(BasicRepaymentPlan::getContractNo, contractNo)
                .eq(BasicRepaymentPlan::getStatus, RepaymentStatusEnum.REPAID)
                .orderByAsc(BasicRepaymentPlan::getTermNo));
        BasicPaymentPool basicPaymentPool = basicPaymentPoolService.getOne(Wrappers.<BasicPaymentPool>lambdaQuery().eq(BasicPaymentPool::getContractNo, contractNo));
        FinanceProductInfoVO vo = new FinanceProductInfoVO();
        vo.setBasicMainInfo(basicMainInfo != null ? basicMainInfo : new BasicMainInfo());
        vo.setBasicFinancialAgreement(basicFinancialAgreement != null ? basicFinancialAgreement : new ArrayList<>());
        vo.setBasicPaymentPool(basicPaymentPool != null ? basicPaymentPool : new BasicPaymentPool());
        vo.setRepaidTermNo(repaidTermNo.size());
        if (EmptyUtils.isNotEmpty(basicFinancialAgreement)) {
            if ("1".equals(basicFinancialAgreement.get(0).getIsSubProduct())) {
                BasicFinancialAgreement agreement = new BasicFinancialAgreement();
                agreement = agreement.sum(basicFinancialAgreement);
                vo.setContractAmt(agreement.getContractAmt());
                vo.setDownPayAmt(agreement.getDownPayAmt());
                vo.setLoanAmt(agreement.getLoanAmt());
                vo.setLoanTerm(agreement.getLoanTerm());
                vo.setCustRate(agreement.getCustRate());
                vo.setMonthPayAmt(agreement.getMonthPayAmt());
            } else {
                BasicFinancialAgreement agreement = new BasicFinancialAgreement();
                agreement = agreement.sum(basicFinancialAgreement);
                vo.setContractAmt(agreement.getContractAmt().add(agreement.getAddAmt()));
                vo.setDownPayAmt(agreement.getDownPayAmt());
                vo.setLoanAmt(agreement.getLoanAmt());
                vo.setLoanTerm(agreement.getLoanTerm());
                vo.setCustRate(agreement.getCustRate());
                vo.setMonthPayAmt(agreement.getMonthPayAmt());
            }

        }
        vo.setPaymentAmt(basicPaymentPool != null ? basicPaymentPool.getPaymentAmt() : b);
        vo.setPaymentStatus(basicPaymentPool != null ? basicPaymentPool.getPaymentStatus() : null);
        for (BasicFinancialAgreement agreement : basicFinancialAgreement) {
            if (agreement.getCostType() == AssetTypeEnum.CAR_AMT) {
                vo.setNotPaidTermNo(agreement.getLoanTerm() - repaidTermNo.size());
            }
        }
        if (EmptyUtils.isNotEmpty(repaidTermNo)) {
            vo.setActualPaymentDate(repaidTermNo.get(repaidTermNo.size() - 1).getActualPaymentDate());
        }

        return new IResponse().setData(vo);
    }

    @ApiOperation(value = "根据合同号码查询金融协议车款信息")
    @PostMapping("/financialAgreeByCar")
    public IResponse financialAgreeByCar(@RequestParam("contractNo") String contractNo) {
        BasicFinancialAgreement basicFinancialAgreement = basicFinancialAgreementService.getOne(Wrappers.<BasicFinancialAgreement>lambdaQuery()
                .eq(BasicFinancialAgreement::getContractNo, contractNo).eq(BasicFinancialAgreement::getCostType, AssetTypeEnum.CAR_AMT));
        return new IResponse().setData(basicFinancialAgreement);
    }

    @ApiOperation(value = "根据合同号码查询金融协议汇总信息(车款与附加贷之和)")
    @PostMapping("/financialAgreeByTotal")
    public IResponse financialAgreeByTotal(@RequestParam("contractNo") String contractNo) {
        List<BasicFinancialAgreement> basicFinancialAgreement = basicFinancialAgreementService.list(Wrappers.<BasicFinancialAgreement>lambdaQuery().eq(BasicFinancialAgreement::getContractNo, contractNo));
        BasicFinancialAgreement agreement = new BasicFinancialAgreement();
        agreement = agreement.sum(basicFinancialAgreement);
        return new IResponse().setData(agreement);
    }


}
